﻿using KELT.KELTObjects.ViewportObjectModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace KELT.Animation.Animation2D
{

    public class ShakeAnimation : IAnimation
    {
        private Rotation3DAnimationUsingKeyFrames rotateAniamtion;
        private int option = 0;

        public void setOption(int option) { this.option = option; }

        public ShakeAnimation()
        {

            rotateAniamtion = new Rotation3DAnimationUsingKeyFrames();

        }

        public override void SetAnimation(Storyboard myStoryboard, ViewportObj obj)
        {
            animationName = "흔들기";


            Vector3D vector3D = new Vector3D(0, 0, 1);

            //회전애니메이션설정
            rotateAniamtion.Duration = TimeSpan.FromSeconds(endTime - startTime);
            rotateAniamtion.BeginTime = TimeSpan.FromSeconds(startTime);

            rotateAniamtion.KeyFrames.Add(
                new LinearRotation3DKeyFrame(
                    new AxisAngleRotation3D(vector3D, 0), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(startTime))) // KeyTime
                );

            rotateAniamtion.KeyFrames.Add(
                new LinearRotation3DKeyFrame(
                    new AxisAngleRotation3D(vector3D, 0), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds((endTime - startTime) / 2))) // KeyTime
                );

            int tempRotate = 20;
            for (int i = 0; i < option; i++)
            {
                tempRotate *= -1;
                rotateAniamtion.KeyFrames.Add(
                    new LinearRotation3DKeyFrame(
                        new AxisAngleRotation3D(vector3D, tempRotate), // Target value (KeyValue)
                        KeyTime.FromTimeSpan(TimeSpan.FromSeconds((endTime - startTime) / 2 + ((endTime - startTime) / option) * i))) // KeyTime
                    );
            }


            rotateAniamtion.KeyFrames.Add(
                new LinearRotation3DKeyFrame(
                    new AxisAngleRotation3D(vector3D, 0), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(endTime))) // KeyTime
                );


            Storyboard.SetTargetName(rotateAniamtion, AnimationInfo.ANIMATION_ROTATE + obj.getIndexNumber());
            Storyboard.SetTargetProperty(rotateAniamtion, new PropertyPath(RotateTransform3D.RotationProperty));
            myStoryboard.Children.Add(rotateAniamtion);
        }
        public override void changeAnimation(Storyboard myStoryboard)
        {
            myStoryboard.Children.Remove(rotateAniamtion);
            rotateAniamtion.Duration = TimeSpan.FromSeconds(endTime - startTime);
            rotateAniamtion.BeginTime = TimeSpan.FromSeconds(startTime);


            rotateAniamtion.KeyFrames.Clear();
            Vector3D vector3D = new Vector3D(0, 0, 1);

            rotateAniamtion.KeyFrames.Add(
                new LinearRotation3DKeyFrame(
                    new AxisAngleRotation3D(vector3D, 0), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(startTime))) // KeyTime
                );

            rotateAniamtion.KeyFrames.Add(
                new LinearRotation3DKeyFrame(
                    new AxisAngleRotation3D(vector3D, 0), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds((endTime - startTime) / 2))) // KeyTime
                );

            int tempRotate = 20;
            for (int i = 0; i < option; i++)
            {
                tempRotate *= -1;
                rotateAniamtion.KeyFrames.Add(
                    new LinearRotation3DKeyFrame(
                        new AxisAngleRotation3D(vector3D, tempRotate), // Target value (KeyValue)
                        KeyTime.FromTimeSpan(TimeSpan.FromSeconds((endTime - startTime) / 2 + ((endTime - startTime) / option) * i))) // KeyTime
                    );
            }


            rotateAniamtion.KeyFrames.Add(
                new LinearRotation3DKeyFrame(
                    new AxisAngleRotation3D(vector3D, 0), // Target value (KeyValue)
                    KeyTime.FromTimeSpan(TimeSpan.FromSeconds(endTime))) // KeyTime
                );

            myStoryboard.Children.Add(rotateAniamtion);
        }


        public override void deleteAnimation(Storyboard myStoryboard)
        {
            myStoryboard.Children.Remove(rotateAniamtion);
        }

    }
}
